/*

Miranda IM: the free IM client for Microsoft* Windows*

Copyright 2000-2007 Miranda ICQ/IM project, 
all portions of this codebase are copyrighted to the people 
listed in contributors.txt.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or ( at your option ) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "commonheaders.h"
#include "ex_import/svcExImport.h"

namespace NExImport {

/***********************************************************************************************************
 * windows procedure
 ***********************************************************************************************************/

/**
 * name:	DlgProcProgress
 * desc:	dialog procedure for the progress dialog
 * params:	none
 * return:	nothing
 **/
LRESULT CALLBACK DlgProcProgress( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam )
{
	switch( msg ) 
	{
		case WM_INITDIALOG:
		{
			const NIcoLib::ICONCTRL idIcon[] = {
				{ ICO_DLG_IMPORT,	WM_SETICON,		NULL		},
				{ ICO_DLG_IMPORT,	STM_SETIMAGE,	ICO_DLGLOGO	},
				{ ICO_BTN_CANCEL,	BM_SETIMAGE,	IDCANCEL	}
			};
			const INT numIconsToSet = DB::Setting::GetByte( SET_ICONS_BUTTONS, 1 ) ? SIZEOF( idIcon ) : 2;
			NIcoLib::SetCtrlIcons( hDlg, idIcon, numIconsToSet );

			TranslateDialogDefault( hDlg );
			SendDlgItemMessage( hDlg, IDCANCEL, BUTTONTRANSLATE, NULL, NULL );
			SendDlgItemMessage( hDlg, IDC_PROGRESS, PBM_SETPOS, 0, 0 );
			SendDlgItemMessage( hDlg, IDC_PROGRESS2, PBM_SETPOS, 0, 0 );
			SetWindowLongPtr( hDlg, GWL_USERDATA, 0 );
			UpdateWindow( hDlg );
			break;
		}
		case WM_CTLCOLORSTATIC:
			switch( GetWindowLong( ( HWND )lParam, GWL_ID ) ) {
				case STATIC_WHITERECT:
				case ICO_DLGLOGO:
				case IDC_INFO:
					SetBkColor( ( HDC )wParam, RGB( 255, 255, 255 ) );
					return ( BOOL )GetStockObject( WHITE_BRUSH );
			}
			return FALSE;
		case WM_COMMAND:
			if( HIWORD( wParam ) == BN_CLICKED ) {
				switch( LOWORD( wParam ) ) {
					case IDCANCEL:
					// in the progress dialog, use the user data to indicate that the user has pressed cancel
					ShowWindow( hDlg, SW_HIDE );
					SetWindowLongPtr( hDlg, GWL_USERDATA, 1 );
					return TRUE;
				}
			}
			break;
	}
	return FALSE;
}

/**
 * name:	CProgress
 * class:	CProgress
 * desc:	create the progress dialog and return a handle as pointer to the datastructure
 * params:	none
 * return:	nothing
 **/
CProgress::CProgress()
{
	_dwStartTime = GetTickCount();
	_hDlg = CreateDialog( ghInst, MAKEINTRESOURCE( IDD_COPYPROGRESS ), 0, ( DLGPROC )DlgProcProgress );
}

/**
 * name:	~CProgress
 * class:	CProgress
 * desc:	destroy the progress dialog and its data structure
 * params:	none
 * return:	nothing
 **/
CProgress::~CProgress()
{
	if( _hDlg ) EndDialog( _hDlg, IDOK );
}

/**
 * name:	SetContactCount
 * class:	CProgress
 * desc:	number of contacts to show 100% for
 * params:	numContacts	- the number of contacts
 * return:	nothing
 **/
VOID CProgress::SetContactCount( DWORD numContacts )
{
	if( _hDlg ) {
		HWND hProgress = GetDlgItem( _hDlg, IDC_PROGRESS2 );
		SendMessage( hProgress, PBM_SETRANGE32, 0, numContacts );   
		SendMessage( hProgress, PBM_SETPOS, 0,  0 );   
	}
}

/**
 * name:	SetSettingsCount
 * class:	CProgress
 * desc:	number of settings & events to show 100% for
 * params:	numSettings	- the number of settings & events
 * return:	nothing
 **/
VOID CProgress::SetSettingsCount( DWORD numSettings )
{
	if( _hDlg ) {
		HWND hProgress = GetDlgItem( _hDlg, IDC_PROGRESS );
		SendMessage( hProgress, PBM_SETRANGE32, 0, numSettings );   
		SendMessage( hProgress, PBM_SETPOS, 0,  0 );   
	}
}

/**
 * name:	Hide
 * class:	CProgress
 * desc:	hides the dialog
 * params:	none
 * return:	nothing
 **/
VOID CProgress::Hide()
{
	ShowWindow( _hDlg, SW_HIDE );
}

/**
 * name:	Update
 * class:	CProgress
 * desc:	update the progress dialog
 * params:	nothing
 * return:  FALSE if user pressed cancel, TRUE otherwise
 **/
BOOLEAN CProgress::Update()
{
	MSG msg;
	
	// show dialog after one second
	if( GetTickCount() > _dwStartTime + 1000 ) {
		ShowWindow( _hDlg, SW_SHOW );
	}

	UpdateWindow( _hDlg );

	while( PeekMessage( &msg, _hDlg, 0, 0, PM_REMOVE ) != 0 ) {
	   if( !IsDialogMessage( _hDlg, &msg ) ) {
		  TranslateMessage( &msg );
		  DispatchMessage( &msg );
	   }
	}
	return GetWindowLongPtr( _hDlg, GWL_USERDATA ) == 0;
}

/**
 * name:	UpdateContact
 * class:	CProgress
 * desc:	increase contact's progressbar by one and set new text
 * params:	pszFormat	- the text to display for the contact
 * return:  FALSE if user pressed cancel, TRUE otherwise
 **/
BOOLEAN CProgress::UpdateContact( LPCTSTR pszFormat, ... )
{
	if( _hDlg != NULL ) {
		HWND hProg = GetDlgItem( _hDlg, IDC_PROGRESS2 );
		if( pszFormat ) {
			TCHAR buf[MAX_PATH];
			va_list vl;
			
			va_start( vl, pszFormat );
			mir_vsntprintf( buf, SIZEOF( buf ), TranslateTS( pszFormat ), vl );
			va_end( vl );
			SetDlgItemText( _hDlg, TXT_CONTACT, buf );   
		}
		SendMessage( hProg, PBM_SETPOS, ( INT )SendMessage( hProg, PBM_GETPOS, 0, 0 ) + 1,  0 );   
		return Update();
	}
	return TRUE;
}

/**
 * name:	UpdateContact
 * class:	CProgress
 * desc:	increase setting's progressbar by one and set new text
 * params:	pszFormat	- the text to display for the setting
 * return:  FALSE if user pressed cancel, TRUE otherwise
 **/
BOOLEAN CProgress::UpdateSetting( LPCTSTR pszFormat, ... )
{
	if( _hDlg != NULL ) {
		HWND hProg = GetDlgItem( _hDlg, IDC_PROGRESS );
		if( pszFormat ) {
			TCHAR buf[MAX_PATH];
			va_list vl;

			va_start( vl, pszFormat );
			mir_vsntprintf( buf, SIZEOF( buf ), TranslateTS( pszFormat ), vl );
			va_end( vl );
			SetDlgItemText( _hDlg, TXT_SETTING, buf );   
		}
		SendMessage( hProg, PBM_SETPOS, ( INT )SendMessage( hProg, PBM_GETPOS, 0, 0 ) + 1,  0 );   
		return Update();
	}
	return TRUE;
}

} // namespace NExImport
